<?php
class Sport{
	// Déclarations des données
	var $id = NULL;
	var $name = NULL;
	var $bookid = NULL;
	
	// Constructeur
	function Sport($pId){
		global $wpdb;
		$_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM sports WHERE id = %d", $pId));
		
		$this->id = $pId;
		$this->name = $_row->name;
		$this->bookid = $_row->bookid;
	}	
	
	// Déclarations des méthodes
	
	//======================
	// Get/Set
	//======================
	
	function getID(){
		return $this->id;
	}
	
	function getName(){		
		return getTranslation('sports', $this->name);
	}
	
	function getBookID(){
		return $this->bookid;
	}
	
	//======================
	// Récupération du nombre de pari disponible pour ce sport
	//======================
	
	function getCountBetsAvailable(){
		global $wpdb;
		return $wpdb->get_var(
				$wpdb->prepare("SELECT count(*) 
								FROM bets b 
								JOIN events e ON b.fk_event = e.id
								JOIN leagues l ON e.fk_league = l.id 								
								WHERE l.fk_sport = ".$this->id."	
								AND	(	( e.startdate > '".date("Y-m-d")."')
										 OR
										( e.startdate = '".date("Y-m-d")."' AND e.starthour > '".date("H:i:s")."')
									)"
				)
			   );
	}
	
	//======================
	// Récupération de la liste des leagues rattachés à ce sport
	//======================
	
	function getLeagues(){
		global $wpdb;		
		return $wpdb->get_results($wpdb->prepare("SELECT id FROM leagues WHERE fk_sport = ".$this->id.""));
	}
	
	//======================
	// Récupération de la liste des events rattachés à ce sport
	//======================
	
	function getEvents(){
		global $wpdb;
		return 
		$wpdb->get_results(
			$wpdb->prepare(
				"SELECT e.id, e.fk_league, e.startdate, e.starthour
				FROM events e
				JOIN leagues l ON e.fk_league = l.id 
				WHERE l.fk_sport = '%d'
				AND (e.startdate > '%s' OR (e.startdate = '%s' AND e.starthour > '%s'))
				ORDER BY e.startdate, e.fk_league, e.starthour",
			
				$this->id,
				date('Y-m-d'),
				date('Y-m-d'),
				date('H:i:s')
			)
		);
	}
}
?>